//-----------------------------------------------------------------------
// <copyright file="GiftDAOBase.cs" company="Pyramid Consulting">
//     Copyright (c) Pyramid Consulting. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Data;
using System.Data.Common;
using System.Collections;
using Microsoft.Practices.EnterpriseLibrary.Data;
using BirthdayReminder.Entities;

using Pyco;
using Pyco.Diagnostics;
using Pyco.Data;

namespace BirthdayReminder.Data
{    
    public abstract class GiftDAOBase
    {
        #region Common methods
        public virtual Gift CreateGiftFromReader(IDataReader reader)
        {
            Gift item = new Gift();
            try
            {
                if (!reader.IsDBNull(reader.GetOrdinal("GiftId"))) item.GiftId = (int)reader["GiftId"];
                if (!reader.IsDBNull(reader.GetOrdinal("Name"))) item.Name = (string)reader["Name"];
                if (!reader.IsDBNull(reader.GetOrdinal("Price"))) item.Price = (int)reader["Price"];
                if (!reader.IsDBNull(reader.GetOrdinal("Description"))) item.Description = (string)reader["Description"];
                if (!reader.IsDBNull(reader.GetOrdinal("CreationDate"))) item.CreationDate = (DateTime)reader["CreationDate"];
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SRBase.DataAccessCreateGiftFromReaderException, ex);
            }
            return item;
        }
        #endregion
		
        #region CreateGift methods
            
        public virtual void CreateGift(Gift gift)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spGiftsInsert");
                
                database.AddInParameter(dbCommand, "@Name", DbType.String, gift.Name);
                database.AddInParameter(dbCommand, "@Price", DbType.Int32, gift.Price);
                database.AddInParameter(dbCommand, "@Description", DbType.String, gift.Description);
                database.AddInParameter(dbCommand, "@CreationDate", DbType.DateTime, gift.CreationDate);
                database.AddOutParameter(dbCommand, "@GiftId", DbType.Int32, 0);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
                gift.GiftId = (int)database.GetParameterValue(dbCommand, "@GiftId");
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SRBase.DataAccessCreateGiftException, ex);
            }
        }

        #endregion
		
        #region UpdateGift methods
        
        public virtual void UpdateGift(Gift gift)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spGiftsUpdate");            
                
                database.AddInParameter(dbCommand, "@GiftId", DbType.Int32, gift.GiftId);
                database.AddInParameter(dbCommand, "@Name", DbType.String, gift.Name);
                database.AddInParameter(dbCommand, "@Price", DbType.Int32, gift.Price);
                database.AddInParameter(dbCommand, "@Description", DbType.String, gift.Description);
                database.AddInParameter(dbCommand, "@CreationDate", DbType.DateTime, gift.CreationDate);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SRBase.DataAccessUpdateGiftException, ex);
            }
        }
        
        #endregion
		
        #region DeleteGift methods
        public virtual void DeleteGift(int giftId)
        {
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spGiftsDelete");
                
                database.AddInParameter(dbCommand, "@GiftId", DbType.Int32, giftId);
                
                DataAccessHelper.ExecuteNonQuery(database, dbCommand);
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SRBase.DataAccessDeleteGiftException, ex);
            }
        }

        #endregion
		
        #region GetGift methods
        
        public virtual Gift GetGift(int giftId)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spGiftsGet");
                
                database.AddInParameter(dbCommand, "@GiftId", DbType.Int32, giftId);
                
                Gift gift =  null;
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    if (reader.Read())
                    {
                        gift = CreateGiftFromReader(reader);
                        reader.Close();
                    }
                }
                return gift;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SRBase.DataAccessGetGiftException, ex);
            }
        }
        
        #endregion
		
        #region GetGiftList methods
        public virtual GiftCollection GetGiftList(GiftColumns orderBy, OrderDirection orderDirection, int page, int pageSize, out int totalRecords)
        {            
            try
            {
                Database database = DatabaseFactory.CreateDatabase();
                DbCommand dbCommand = database.GetStoredProcCommand("spGiftsGetList");
                
                database.AddInParameter(dbCommand, "@OrderBy", DbType.AnsiString, orderBy.ToString());
                database.AddInParameter(dbCommand, "@OrderDirection", DbType.AnsiString, orderDirection.ToString());
                database.AddInParameter(dbCommand, "@Page", DbType.Int32, page);
                database.AddInParameter(dbCommand, "@PageSize", DbType.Int32, pageSize);
                database.AddOutParameter(dbCommand, "@TotalRecords", DbType.Int32, 4);
                
                GiftCollection giftCollection = new GiftCollection();
                using (IDataReader reader = DataAccessHelper.ExecuteReader(database, dbCommand))
                {
                    while (reader.Read())
                    {
                        Gift gift = CreateGiftFromReader(reader);
                        giftCollection.Add(gift);
                    }
                    reader.Close();
                }
                totalRecords = (int)database.GetParameterValue(dbCommand, "@TotalRecords");
                return giftCollection;
            }
            catch (Exception ex)
            {
                // log this exception
                Logger.Error(ex);
                // wrap it and rethrow
                throw new DataAccessException(SRBase.DataAccessGetGiftListException, ex);
            }
        }
        
        public virtual GiftCollection GetGiftList(GiftColumns orderBy, OrderDirection orderDirection)
        {            
            int totalRecords = 0;
            return GetGiftList(orderBy, orderDirection, 0, 0, out totalRecords);
        }
        
        #endregion
		
    }
}